package com.ljxt.dt.web.servlet.storemanagementservlet;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ljxt.dt.service.storemanagementservice.AddReceptService;
import com.ljxt.dt.service.storemanagementservice.impl.AddReceptServiceImpl;
import com.ljxt.dt.vo.JsonResult;
import com.ljxt.dt.vo.storemanagement.JsonBen;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-17 15:30
 * @copyright 老九学堂
 */
@WebServlet("/addBill")
public class AddReceptBillServlet extends HttpServlet {
    private AddReceptService admin = new AddReceptServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建一个byte 通过字符流得到前端的json对象的字符串
        byte[] buffer = new byte[req.getContentLength()];

        req.getInputStream().read(buffer,0,req.getContentLength());
        String str = new String(buffer,"UTF-8");

        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();

        JsonBen jsonBean = gson.fromJson(str, JsonBen.class);
        //使用第三方的工具将字符串转为对象


        ObjectMapper objectMapper = new ObjectMapper();
      //添加入库单
       int row =  admin.insertRecept(jsonBean);

       resp.getWriter().write(objectMapper.writeValueAsString(row));
    }
}
